package oriol.forns.gutierrez;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class Objecte {

	public float x;
	public float y;
	public float vx;
	public float vy;
	public float tx;
	public float ty;
	public Bitmap img;
	public String tipus;
	//Sprites
	private boolean sprite; //es sprite o no
	private int ih;		//numero d'imatges horitzontals a l'sprite
	private int iv;		//numero d'imatges verticals a l'sprite
	private int sfcount;	//sprite frame count
	private int fpi;		//frames per imatge
	private Rect src;
	private Rect dest;
	public boolean acabat;
	
	public Objecte(float nx, float ny, float nvx, float nvy, float ntx, float nty, Bitmap imgn) {
		x = nx;
		y = ny;
		vx = nvx;
		vy = nvy;
		tx = ntx;
		ty = nty;
		img = imgn;
		sprite = false;
		img = Bitmap.createScaledBitmap(imgn,(int)tx,(int)ty,false);
	}
	
	public Objecte(float nx, float ny, float nvx, float nvy, float ntx, float nty, Bitmap imgn, String nom) {
		x = nx;
		y = ny;
		vx = nvx;
		vy = nvy;
		tx = ntx;
		ty = nty;
		img = imgn;
		sprite = false;
		tipus = nom;
		img = Bitmap.createScaledBitmap(imgn,(int)tx,(int)ty,false);
	}
	
	public Objecte(Bitmap bm, float nx, float ny, float ntx, float nty, float nvx, float nvy, int nih, int niv, int nfpi) {
		x = nx;
		y = ny;
		tx = ntx;
		ty = nty;
		vx = nvx;
		vy = nvy;
		sprite = true;
		acabat = false;
		ih = nih;
		iv = niv;
		sfcount = -1;
		fpi = nfpi;
		src = new Rect();
		dest = new Rect();
		img = Bitmap.createScaledBitmap(bm,(int)tx*ih,(int)ty*iv,false);
	}
	
	public void actualitza() {
		x += vx;
		y += vy;
		if(sprite && sfcount/fpi>(ih*iv)) acabat = true;
	}
	
	public void nouimg(Bitmap nou){
		img = Bitmap.createScaledBitmap(nou,(int)tx,(int)ty,false);
	}
	
	public void reescala(){
		img = Bitmap.createScaledBitmap(img,(int)tx,(int)ty,false);
	}
	
	public void draw(Canvas canvas) {
		if (!sprite) canvas.drawBitmap(img, x, y, null);
		else {
			int s = sfcount/fpi;
			src.set((int)((s%ih)*tx), (int)((s/iv)*ty), (int)((s%ih)*tx+tx), (int)((s/iv)*ty+ty));
			dest.set((int)x,(int)y,(int)(x+tx),(int)(y+ty));
			canvas.drawBitmap(img, src, dest, null);
			sfcount++;
		}
		
	}

}
